package view;

import main.WinkelApplication;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import model.Product;

public class BasketDisplay extends JPanel implements ActionListener, Observer 
{
    public BasketDisplay() 
    {
        super();
        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setBackground(new Color(151, 186, 225));
        
        // TODO: Change layout of parent to prevent absolute sizing and stuff
        setSize(dim.width / 5, dim.height - 40);
        setLocation(dim.width - ((dim.width / 5) + 20), 20);
        
        WinkelApplication.getBasket().addObserver(this);
        
        initComponents();
    }

    private void initComponents() 
    {
        removeAll();
        
        final model.Basket basket = WinkelApplication.getBasket();

        JLabel lblTitle = new JLabel("Winkelmand", SwingConstants.CENTER);
        lblTitle.setFont(WinkelApplication.FONT_16_BOLD);
      
        add(lblTitle);

        int containerWidth = getWidth();
        
        JPanel productsContainer = new JPanel();
        
        // Set the size of the container because the parent doesn't have
        // a layout
        productsContainer.setSize(containerWidth, 0);

        ClassLoader cldr = this.getClass().getClassLoader();
        URL imageURL   = cldr.getResource("view/cancel.png");
        ImageIcon icon = new ImageIcon(imageURL);
        
        // Add all of the items currently in the basket
        for (final Product product : basket.getProducts()) 
        {
            JPanel itemContainer = new JPanel();
            itemContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
            itemContainer.setPreferredSize(new Dimension(containerWidth, 26));
            itemContainer.setOpaque(false);
            
            // "Amount - productnamegoeshere"
            JLabel lblProduct = new JLabel(
                    basket.getProductAmount(product) +
                    " - " + product.toString()
                );
            lblProduct.setFont(WinkelApplication.FONT_10_PLAIN);
            lblProduct.setPreferredSize(new Dimension(containerWidth - 120, 20));
            
            JLabel lblPrice = new JLabel(
                    WinkelApplication.CURRENCY + WinkelApplication.PRICE_FORMAT.format(product.getPrice())
                );
            lblPrice.setFont(WinkelApplication.FONT_10_PLAIN);
            lblPrice.setPreferredSize(new Dimension(100, 20));
            
            if(basket.getProductAmount(product)>product.getVoorraad()){
                lblProduct.setForeground(Color.red);
                lblPrice.setForeground(Color.red);
            }
            
            JButton removeButton = new JButton(icon);
            removeButton.setPreferredSize(new Dimension(20, 20));
            removeButton.setBorderPainted(false);
            removeButton.setContentAreaFilled(false);
            removeButton.setFocusPainted(false);
            removeButton.setOpaque(false);
            
            removeButton.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent e)
                {
                   //Execute when button is pressed
                   WinkelApplication.getBasket().removeOneProduct(product);
                   initComponents();
                   revalidate();
                   repaint();
                }
            });            
            
            itemContainer.add(removeButton);
            itemContainer.add(lblProduct);
            itemContainer.add(lblPrice);            
            
            productsContainer.add(itemContainer);
        }

        productsContainer.setOpaque(false);
        productsContainer.setVisible(true);
        
        add(productsContainer);
        
        JLabel lblTotal = new JLabel("Totaal: ");
        lblTotal.setFont(WinkelApplication.FONT_16_BOLD);
        
        JLabel lblTotalPrice = new JLabel(WinkelApplication.CURRENCY + 
                WinkelApplication.PRICE_FORMAT.format(basket.getTotalCosts()));
        lblTotalPrice.setFont(WinkelApplication.FONT_16_BOLD);
        
        JButton btnGoToPay = new JButton("Betalen...");
        btnGoToPay.setFont(WinkelApplication.FONT_12_BOLD);
        btnGoToPay.addActionListener(this);
        
        if (basket.size() < 1)
            btnGoToPay.setEnabled(false);
        
        add(lblTotalPrice);
        add(lblTotal);
        
        add(btnGoToPay);
    }

    @Override
    public void update(Observable observable, Object arg) {
        initComponents();
        revalidate();
        repaint();
    }

    
    @Override
    public void actionPerformed(ActionEvent event) {
        WinkelApplication.getInstance().showPanel(new Payment());
    }
}
